package com.cqut.hfh.ordering.controller;

import com.cqut.hfh.ordering.pojo.EhyManager;
import com.cqut.hfh.ordering.pojo.EhyRole;
import com.cqut.hfh.ordering.pojo.MerchantType;
import com.cqut.hfh.ordering.service.IMerchantTypeService;
import com.cqut.hfh.ordering.utils.EntityIdutils;
import com.cqut.hfh.ordering.utils.Message;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * create by gtt on 2019/6/26
 */
@Controller
@RequestMapping("/merchantType")
public class MerchantTypeController {

    @Resource
    private IMerchantTypeService merchantTypeService;

    /**
     * 获取所有商家类型
     * @param limit
     * @param page
     * @return
     */
    @RequestMapping(path = "/findAlls",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String findAlls(@RequestParam("limit") String limit,
                           @RequestParam("page") String page){
        List<Map<String, Object>> merchanttype_list = merchantTypeService.getMerchantType(limit, page);
        for( Map<String, Object> map : merchanttype_list){
            if( map.get("isva").toString().equals("1")){
                map.put("isva", "有效");
            }
            if( map.get("isva").toString().equals("0")){
                map.put("isva", "无效");
            }
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String tsStr = sdf.format(map.get("optime"));
            map.put("optime", tsStr);
        }
        Integer count = merchantTypeService.getCount();
        return Message.getReturnMsg(0,"", count, merchanttype_list);
    }

    /**
     * 显示所有的商家类型
     * @return
     */
    @RequestMapping(path = "/findAll",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String findAll(){
        List<MerchantType> result_map = merchantTypeService.getType();
        return Message.getReturnMsg(0,"",result_map);
    }

    /**
     * 通过类型名称查找该类型信息
     * @param limit
     * @param page
     * @param type_name
     * @return
     */
    @RequestMapping(path = "/findMerchanTypeByTypeName",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String findMerchanTypeByTypeName(@RequestParam("limit") String limit,
                                            @RequestParam("page") String page,
                                            @RequestParam("type_name") String type_name){
        List<Map<String, Object>> merchanttype_list = merchantTypeService.getMerchantTypeByName(limit, page, type_name);

        for( Map<String, Object> map : merchanttype_list){
            if( map.get("isva").toString().equals("1")){
                map.put("isva", "有效");
            }
            if( map.get("isva").toString().equals("0")){
                map.put("isva", "无效");
            }
            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String tsStr = sdf.format(map.get("optime"));
            map.put("optime", tsStr);
        }

        Integer count = merchantTypeService.getCount();

        return Message.getReturnMsg(0,"", count, merchanttype_list);
    }

    /**
     * 通过类型id删除商品类型，以及商品表的商品
     * @param typeId
     * @return
     */
    @RequestMapping(path = "/deletes",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String deleById(@RequestParam("typeId") String typeId){
        int temp = merchantTypeService.delById(typeId);
        if( temp == 1){
            return "{\"msg\":\"ok\"}";
        }
        return "{\"msg\":\"fail\"}";
    }

    /**
     * 通过类型id查找本类型信息
     * @param typeId
     * @return
     */
    @RequestMapping("/findById")
    @ResponseBody
    public MerchantType findById(String typeId){
        Map<String, Object> result_map = merchantTypeService.findById(typeId);
        MerchantType e = new MerchantType();
        e.setProperties(result_map);
        return e;
    }

    /**
     * 修改商品的类型数据
     * @param merchantType
     * @return
     */
    @RequestMapping(path = "/update")
    @ResponseBody
    public String update(MerchantType merchantType){
        merchantType.setOptime(new Date());
        if( merchantType.getType_id() != null) {
            int temp = merchantTypeService.updateMerchantType(merchantType);
        }
        return "";
    }

    /**
     * 添加商品类型
     * @param merchantType
     * @return
     */
    @RequestMapping(path = "/save",produces = {"text/html;charset=utf-8"})
    @ResponseBody
    public String save(MerchantType merchantType){
        merchantType.setOptime(new Date());
        if( merchantType.getType_id() == null) {
            merchantType.setType_id(EntityIdutils.getId());
            int temp = merchantTypeService.saveMerchantType(merchantType);
            if (temp==1) {
                return Message.getReturnMsg(0, "");
            }
        }
        return "";
    }
}
